<?php

/*
 * 员工中心
 */
namespace App\Http\Controllers\Tenement;

use App\Common\json;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Console\EnvironmentCommand;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class EmployeeController extends Controller{

    public function wellbad($id){
        $data       = DB::table('servicereview') ->select('well','bad') ->where('hashslinger_servicereview',$id)->get();
        return $data;
    }


    public function SubmitEmployee(Request $request){
        if($request ->isMethod('post')){
            $village_id     = $request ->input('value');
            $data       = DB::table('hashslinger')
             //   ->select('name','hashslinger_id','hashslinger_head','position'/*,'well','bad'*/)
                ->leftjoin('servicereview','hashslinger.hashslinger_id','=','servicereview.hashslinger_servicereview')
                ->where('name', 'like','%'.$village_id.'%')
                ->get();
            foreach ($data as $value){

                $da[] = array('hashslinger_id'=>$value->hashslinger_id,
                    'hashslinger_head' => $value->hashslinger_head,
                    'position' => $value->position,
                    'name'      =>$value->name,
                    'rating'=> $value->well);
            }
            if ($data == true) {
                return json::json('success', '获取搜索内容成功', $da);
            } else {
                return json::json('failure', '您搜索的内容不存在', []);
            }
        }
    }


    public function Employee($id){
        $data   = DB::table('servicereview') ->where('hashslinger_servicereview',$id) ->get();
        foreach ($data as $value){
            $well[]   = $value ->well;
            $bad[]    = $value ->bad;
            $iw   = count(array_filter($well));
            $ib   = count(array_filter($bad));
            $sum     = round((array_sum($well)+array_sum($bad))/($iw+$ib));
        }
        if(empty($sum)){
            return "0";
        }else{
            return $sum;
        }

    }

    //获取全部员工信息
    public function AllEmployee(Request $request){
        if($request ->isMethod('post')){
            $village_id     = $request ->input('village_id');
            $data       = DB::table('hashslinger')
                ->where('village_hashslinger',$village_id)
                ->get();

            foreach ($data as $row) {
                $hashslinger_id=$row->hashslinger_id;
                $hashslinger_head = $row->hashslinger_head;
                $position = $row->position;
                $name      =$row->name;
                $ens      = $this ->Employee($hashslinger_id);
                $da[]     = array(
                    'hashslinger_id'  =>$hashslinger_id,
                    'hashslinger_head' =>$hashslinger_head,
                    'position'          => $position,
                    'name'              => $name,
                    'rating'            => $ens
                );
            }

            if(empty($da)){
                $ad   = array();
            }else{
                $ad  = $da;
            }
            if ($data == true) {
                return json::json('success', '获取全部员工数据', $ad);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', '');
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //评论
    public function comment($id){
        $data          = DB::table('servicereview')
                        ->leftjoin('user','servicereview.user_servicereview','=','user.user_id')
                       ->where('hashslinger_servicereview',$id)
                       ->get();
        return $data;
    }

    //服务人员详情
    public function SelectEmployee(Request $request){
        if($request ->isMethod('post')){
            $hashslinger_id   = $request ->input('hashslinger_id');  //服务人员ID
            $data       = DB::table('hashslinger')
              //  ->select('name','hashslinger_id','hashslinger_head','position','well','bad','comment')
                ->leftjoin('servicereview','hashslinger.hashslinger_id','=','servicereview.hashslinger_servicereview')
                ->where('hashslinger_id',$hashslinger_id)
                ->get();
            foreach ($data as $value){
                $user_id     = $value ->hashslinger_id;
               /* $id  = $value ->well;
                $rating   = array();
                if($id>=0 && $id <=100){
                    $rating  = 1;
                }elseif ($id>=101 && $id <=500){
                    $rating  = 2;
                }elseif ($id>=501 && $id <=1000){
                    $rating  = 3;
                }elseif ($id>=1001 && $id <= 1500){
                    $rating  = 4;
                }else{
                    $rating  = 5;
                }*/


                $rating= $value ->well;
               // $sum     = ceil(array_sum($rating)/count($rating));

            }
            $comment    = $this->comment($user_id);
            foreach ($comment as $key){
                $head     = $key ->head;
                $nickname  = $key ->nickname;
                $time      = $key ->serviceview_create_time;
                $comments  = $key ->comment;

                $list [] = array('head'=>$head,'nickname'=>$nickname,'time'=>$time,'comment'=>$comments);
            }
            if(empty($list)){
                $dp     = array();
            }else{
                $dp     = $list;
            }
            $da = array('hashslinger_id'=>$value->hashslinger_id,
                'name'  =>$value ->name,
                'hashslinger_head' => $value->hashslinger_head, 'position' => $value->position,'rating'=> $rating,
                'list'=>$dp);
            if ($data == true) {
                return json::json('success', '获取服务人员详情成功', $da);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', '');
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }




}
